/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import java.io.Serializable;
import java.text.StringCharacterIterator;

/**
 *
 * @author darkdev
 */
public class UserData implements Serializable {

    /**
     * The user's name.
     * @serial
     */
    private String userName;
    /**
     * The user's password.
     * @serial
     */
    private String userPassword;

    public UserData(
            String UserName, String Password) {
        super();
        setUserName(UserName);
        setPassword(Password);
    }
    //if no password required,
    public UserData()
    {
        super();
    }

    /**
     * Names must contain only letters, spaces, and apostrophes.
     * Validate before setting field to new value.
     *
     *
     */
    public String setUserName(String newUserName) {
        try{
        validateName(newUserName);
        }
        catch(IllegalArgumentException il)
        {
            return il.getMessage();
        }
        this.userName = newUserName;
        return "";
    }

    public String setPassword(String newPassword) {
        boolean ok = validatePassword(newPassword);
        if(!ok)
            return "Password must have at list 4 characters,  one digit, one upper and lower case";
        this.userPassword = newPassword;
        return "";
    }

    public String getUserName()
    {
        return this.userName;
    }
    /**
     * Ensure names contain only letters, spaces, and apostrophes.
     *
     * @throws IllegalArgumentException if field takes an unpermitted value.
     */
    private void validateName(String aName) {
        boolean nameHasContent = (aName != null) && (!aName.equals(""));
        if (!nameHasContent) {
            throw new IllegalArgumentException("Names must be non-null and non-empty.");
        }

        StringCharacterIterator iterator = new StringCharacterIterator(aName);
        char character = iterator.current();
        while (character != StringCharacterIterator.DONE) {
            boolean isValidChar =
                    (Character.isLetter(character)
                    || Character.isSpaceChar(character)
                    || character == '\'');
            if (isValidChar) {
                //do nothing
            } else {
                String message = "Names can contain only letters, spaces, and apostrophes.";
                throw new IllegalArgumentException(message);
            }
            character = iterator.next();
        }
    }

    private boolean validatePassword(String newPassword) {
        String pattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{4,})";
        boolean matches = newPassword.matches(pattern);
        return matches;
    }
}
